//
//  GStepView.m
//  helpinrain
//
//  Created by Gang.Wang on 12-7-27.
//  Copyright (c) 2012年 Zhuhai Yuike Technology Co., Ltd. All rights reserved.
//

#import "GStepView.h"
#import "GStepXmlModel.h"
#import "mcf.h"
#import "ContentItemObject.h"

@interface GStepView()
{
    UIViewEx * contentView_;
    
    UILabel * title_;
    LineView * lineView_;
    
    NSMutableDictionary * contentViews_;
    
}


@property (nonatomic, strong) UILabel * title;
@property (nonatomic, strong) LineView * lineView;

@property (nonatomic, strong) NSMutableDictionary * contentViews;
@property (nonatomic ,strong) UIViewEx * contentView;

- (void) privateInit;
- (void) showData2View;
- (UIView *) generateContentView:(ContentItemObject *) content;

@end

@implementation GStepView

@synthesize stepData = stepData_;

@synthesize title = title_;
@synthesize lineView = lineView_;

@synthesize contentViews = contentViews_;
@synthesize contentView = contentView_;

- (void) dealloc
{
    YRelease(stepData_)
    
    YRelease(title_)
    YRelease(lineView_)
    
    YRelease(contentViews_);
    YRelease(contentView_)
    
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self reAdjustLayout];
}

- (float) contentViewHeight:(int) contentViewIndex
{
    float result = 100;
    
    if (contentViewIndex < 0 ||
        contentViewIndex >= [self.stepData.contents count])
    {
        return result;
    }
    
    ContentItemObject * contentData = [self.stepData.contents objectAtIndex:contentViewIndex];
    
    if (contentData.contentType == kContentItemObjectType_Image)
    {
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", contentData.content]];
        if (!image)
        {
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", contentData.content]];
        }
        
        if (!image)
        {
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpeg", contentData.content]];
        }
        
        return image.size.height;
    }
    else
    {
        CGSize textSize = [contentData.content sizeforFont:[UIFont systemFontOfSize:isPad ? 20 : 12] width:self.width linebreak:UILineBreakModeWordWrap];
        

        
        result = textSize.height;
    }
    
    return result;
}

- (void) reAdjustLayout
{
    [self.contentView setFrame:CGRectMake(15, 0, self.width - 30, self.height)];
    
    [self.title setFrame:CGRectMake(0, 0, self.width, 40)];
    [self.lineView setFrame:CGRectMake(0, 40, self.width, 2)];
    
    
    float origin_y___ = self.lineView.origin_y + self.lineView.height;
    
    origin_y___ += isPad ? 15 : 5;
    for (int i = 0; i < [self.contentViews count]; i++)
    {
        NSString * key = [NSString stringWithFormat:@"%d", i];
        UIView * view = [self.contentViews objectForKey:key];
        
        if (!view)
        {
            continue;
        }
        
        float height = [self contentViewHeight:i];
        CGRect frame = CGRectMake(0, origin_y___, self.contentView.width, height);
        frame.origin.y = origin_y___;
        [view setFrame:frame];
        
        
        origin_y___ += frame.size.height;
        origin_y___ += isPad ? 15 : 5;
        
    }
    
    
}


- (void) setStepData:(GStepXmlModel *)stepDataValue
{
    YRelease(stepData_)
    stepData_ = [stepDataValue retain];
    
    [self showData2View];
}

#pragma mark - Private Method
- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    [self setBackgroundColor:COLOR_WHITE];
    
    [[self layer] setCornerRadius:isPad? 10 : 5];
    [self setClipsToBounds:TRUE];
    
    UIViewEx * temp_view = [[UIViewEx alloc] init];
    self.contentView = temp_view;
    YRelease(temp_view)
    [self addSubview:self.contentView];
    
    UILabel * temp_title = [[UILabel alloc] init];
    [temp_title setTextAlignment:UITextAlignmentLeft];
    [temp_title setBackgroundColor:COLOR_CLEAR];
    [temp_title setFont:[UIFont systemFontOfSize:isPad ? 20 : 12]];
    
    self.title = temp_title;
    YRelease(temp_title)
    [self.contentView addSubview:self.title];
    
    LineView * temp_line = [[LineView alloc] init];
    self.lineView = temp_line;
    YRelease(temp_line)
    [self addSubview:self.lineView];
    
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}


- (void) showData2View
{
    if (!self.stepData)
    {
        return;
    }
    
    if ([NSArray isNilOrEmpty:self.stepData.contents])
    {
        return;
    }
    
    [self.title setText:self.stepData.name];
    
    if (!self.contentViews)
    {
        NSMutableDictionary * temp_dic = [[NSMutableDictionary alloc] init];
        self.contentViews = temp_dic;
        YRelease(temp_dic)
    }
        
    int index = 0;
   
    for (ContentItemObject * itemContent in self.stepData.contents) {
        if (!itemContent)
        {
            continue;
        }
        
        UIView * contentView = [self generateContentView:itemContent];
        if (!contentView)
        {
            continue;
        }
        NSString * dic_key = [NSString stringWithFormat:@"%d", index];

        [self.contentViews setObject:contentView forKey:dic_key];
        [self.contentView addSubview:contentView];
           
        index++;
        
    }
    
    [self reAdjustLayout];
    
}


- (UIView *) generateContentView:(ContentItemObject *)content
{
    if (!content)
    {
        return nil;
    }

    if (content.contentType == kContentItemObjectType_Text)
    {
        UILabel * label = [[UILabel alloc] init];
        [label setTextAlignment:UITextAlignmentLeft];
        [label setFont:[UIFont systemFontOfSize:isPad ? 16 : 10]];
        [label setTextColor:COLOR_BLACK];
        [label setBackgroundColor:COLOR_CLEAR];
        label.lineBreakMode = UILineBreakModeWordWrap;
        label.numberOfLines = 0;
        
        [label setText:content.content];
        
        return [label autorelease];
    }
    
    
    if (content.contentType == kContentItemObjectType_Image)
    {
        UIImageView * imageView = [[UIImageView alloc] init];
        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", content.content]];
        if (!image)
        {
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", content.content]];
        }
        
        if (!image)
        {
            image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpeg", content.content]];
        }
        
        [imageView setImage:image];
        return [imageView autorelease];
    }
    
    
    
    return nil;   

}



- (float) needHeight
{
    float result = 0;
    
    
    float origin_y___ = self.lineView.origin_y + self.lineView.height;
    
    origin_y___ += 15;
    
    NSString * key = [NSString stringWithFormat:@"%d", [self.contentViews count] - 1];
    
    UIView * view = [self.contentViews objectForKey:key];
    result = view.frame.origin.y + view.frame.size.height + 15.0f;
    
    
    return result == 0? 300 : result;
}




















@end
